package cn.maihe.elg.operation.centers.epoint.channel;

import cn.maihe.elg.operation.centers.epoint.channel.service.LYEpointBaohanNoticeChannelService;
import cn.maihe.elg.operation.centers.epoint.channel.service.LYUploadBaohanFileChannelService;
import cn.maihe.elg.operation.centers.epoint.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.dto.channel.LYEpointBaohanNoticeReqDTO;
import cn.maihe.elg.operation.centers.epoint.enums.EpointRespCodeEnum;
import cn.maihe.elg.operation.centers.supports.model.bo.InnerUploadBaohanFileBo;
import cn.maihe.elg.operation.centers.supports.model.bo.NoticeGuaranteeApplyResultBo;
import cn.maihe.elg.operation.centers.supports.model.bo.NoticePayResultBo;
import cn.maihe.elg.operation.centers.supports.model.bo.PushCenterGuaranteeBo;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.BidCustomerDO;
import cn.maihe.elg.operation.repository.entity.GuaranteeVo;
import cn.maihe.elg.operation.service.business.BidCustomerService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description 对接龙岩服务中心
 * @Author Alan
 * @Date 2025/02/12
 */
@Service
@Slf4j
public class LYCenterInterfaceServiceImpl extends BaseEpointCenterInterfaceService {

    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private BidCustomerService bidCustomerService;
    @Resource
    private LYEpointBaohanNoticeChannelService lyEpointBaohanNoticeChannelService;
    @Resource
    private LYUploadBaohanFileChannelService lyUploadBaohanFileChannelService;

    @Override
    public CenterNoEnum getCenterNoEnum() {
        return CenterNoEnum.LONGYAN;
    }

    @Override
    public InnerBaseResp<NoticeGuaranteeApplyResultBo> noticePayResult(NoticePayResultBo bo) {
        return InnerBaseResp.success(null, "无需 电子保函支付结果通知");
//        throw new RuntimeException(getCenterNoEnum().getCenterName() + " 暂未实现 电子保函支付结果通知");
    }

    @Override
    public InnerBaseResp<String> pushGuarantee(PushCenterGuaranteeBo bo) {
        GuaranteeVo vo = bo.getGuaranteeVo();
        AcceptOrderInfoDO acceptOrderInfo = acceptOrderInfoService.getByAcceptOrderNo(vo.getAcceptOrderNo());
        Assert.notNull(acceptOrderInfo, "查无此受理订单记录");
        BidCustomerDO bidCustomerDO = bidCustomerService.selectById(acceptOrderInfo.getBidCustomerId());
        String applyStatus = bo.getApplyStatus();
        LYEpointBaohanNoticeReqDTO reqDTO = new LYEpointBaohanNoticeReqDTO();
        reqDTO.setApplyno(vo.getCenterOrderId());
        reqDTO.setInstname(vo.getGuarantorName());
        reqDTO.setBaohanno(vo.getGuaranteeNo());
        if (vo.getGuaranteeTime() == null) {
            vo.setGuaranteeTime(new Date());
        }
        reqDTO.setGeneratetime(DateUtil.formateDate(vo.getGuaranteeTime(), "yyyy-MM-dd HH:mm:ss"));
//        reqDTO.setGeneratetime(vo.getGuaranteeTime() != null ? DateUtil.formateDate(vo.getGuaranteeTime(), "yyyy-MM-dd HH:mm:ss") : null);
        reqDTO.setApplystatus(applyStatus);
        reqDTO.setCost(fen2yuanString(Long.valueOf(vo.getAmount())));
        //0.01% 转0.0001
        reqDTO.setRate(getPercentageToDecimal(vo.getRate()));
        reqDTO.setPayername(vo.getBidCustomerName());
        if (bidCustomerDO != null) {
            reqDTO.setPayeraccount(bidCustomerDO.getLicenseAccount());
        }
        InnerBaseResp<String> innerBaseResp = null;

//        AcceptOrderInfoDO acceptOrderInfo = acceptOrderInfoService.getByAcceptOrderNo(vo.getAcceptOrderNo());
//        Assert.notNull(acceptOrderInfo, "查无此受理订单记录");
        try {
            EpointBaseRespDTO resp = lyEpointBaohanNoticeChannelService.doRequest(getCenterNoEnum(), reqDTO);
            if (EpointRespCodeEnum.isSuccess(resp.getCode())) {
                innerBaseResp = InnerBaseResp.success(reqDTO.getBaohanno());
            } else {
                innerBaseResp = InnerBaseResp.error(resp.getMessage());
            }
        } catch (Exception e) {
            log.error("开函结果通知 >> [{}] 异常:{}", getCenterNoEnum().getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }

        return innerBaseResp;
    }

    @Override
    public InnerBaseResp uploadBaohanFile(String acceptOrderNo, InnerUploadBaohanFileBo bo) {
        return lyUploadBaohanFileChannelService.uploadBaohanFile(acceptOrderNo, getCenterNoEnum(), bo);
    }
}
